package com.sannuo.manager.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sannuo.common.domain.UserLogin;
import com.sannuo.common.domain.bo.Login;
import com.sannuo.common.domain.bo.Xtzh;
import com.sannuo.common.domain.vo.XtzhVo;
import com.sannuo.common.domain.vo.XtzhVo1;
import com.sannuo.common.domain.vo.XtzhVo2;
import com.sannuo.common.result.Result;
import com.sannuo.common.result.ResultCode;
import com.sannuo.manager.service.XtzhService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RequestMapping("/xlh")
@RestController
@Api(tags = "系统账号管理",description = "业务管理")
@CrossOrigin
public class XtzhController {

    @Autowired
    private XtzhService xtzhService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ApiOperation(value = "系统账户查询", notes = "根据条件分页查询", httpMethod = "GET")
    private Result fin(Integer page,Integer limit,String searchParams) throws Exception{

        ObjectMapper objectMapper = new ObjectMapper();
        XtzhVo xtzhVo = null;
        if (searchParams != null){
            xtzhVo = objectMapper.readValue(searchParams, XtzhVo.class);
        }

        Page page1 = PageHelper.startPage(page, limit);

        List<Xtzh> select = xtzhService.select(xtzhVo);

        PageInfo pageInfo = new PageInfo<>(select);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(),select);
    }

    @RequestMapping(value = "xzthadd", method = RequestMethod.POST)
    @ApiOperation(value = "系统账户添加", notes = "系统账户添加", httpMethod = "POST")
    public Result addfin(XtzhVo2 xtzhVo2) throws Exception{
        String str="abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<10;i++){
            int number = random.nextInt(36);
            sb.append(str.charAt(number));
        }
        xtzhVo2.setCustomerCode(sb.toString());
        int i = xtzhService.addUserLogin(xtzhVo2);
        System.out.println(xtzhVo2+"——"+i);
        if(i>0){
            int i1 = xtzhService.addUserStore(xtzhVo2);
            int i2 = xtzhService.addUserRole(xtzhVo2);
        }

        return Result.ok(ResultCode.SUCCESS,null);
    }

    @RequestMapping(value = "gh", method = RequestMethod.POST)
    @ApiOperation(value = "系统账户修改", notes = "系统账户修改", httpMethod = "POST")
    private Result updfin(XtzhVo1 xtzhVo1) throws Exception{
        System.out.println(xtzhVo1);
        int i = xtzhService.updateXtzh(xtzhVo1);
        return Result.ok(ResultCode.SUCCESS,i);
    }

    @RequestMapping(value = "xg", method = RequestMethod.POST)
    @ApiOperation(value = "密码修改", notes = "密码修改", httpMethod = "POST")
    private Result atefin(Login login) throws Exception{
        System.out.println(login);

        int i=0;
        if(login.getPassword2().equals(login.getPassword3())){
            i = xtzhService.updateUserLogin(login);
        }else{
            System.out.println("密码不一致");
            return Result.fail(ResultCode.FAIL,i);
        }

        if(i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else{
            System.out.println("原密码错误");
            return Result.fail(ResultCode.FAIL,i);
        }

    }

    @RequestMapping(value = "xougai",method = RequestMethod.POST)
    @ApiOperation(value = "", notes = "",httpMethod = "POST")
    public Result ateadd(UserLogin userLogin){
        System.out.println(userLogin);
        int i = xtzhService.updUserLogin(userLogin);
        return Result.ok(ResultCode.SUCCESS,i);
    }


    @RequestMapping(value = "aaa", method = RequestMethod.POST)
    @ApiOperation(value = "修改状态", notes = "修改状态", httpMethod = "POST")
    private Result qwerty(UserLogin login) throws Exception{
        System.out.println(login);
        if (login.getStatus() == 1) {
            login.setStatus(0);
        } else {
            login.setStatus(1);
        }
        int i = xtzhService.updUser(login);
        return Result.ok(ResultCode.SUCCESS,i);
    }


}
